package com.photologics.logic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.photologics.main.R;

public class SelectLevel_Adapter extends ArrayAdapter<String> {

	private Context context;
	private final String[] logiclevel;

	public SelectLevel_Adapter(Context context, String[] logiclevel) {
		super(context, R.layout.select_logic_level, logiclevel);
		this.context = context;
		this.logiclevel = logiclevel;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View listView = inflater.inflate(R.layout.select_logic_level, parent,
				false);
		TextView textView = (TextView) listView.findViewById(R.id.logic_level);
		ImageView imageView = (ImageView) listView
				.findViewById(R.id.logicprocessing_image);

		textView.setText(logiclevel[position]);

		// Change icon based on name
		String s = logiclevel[position];

		System.out.println(s);

		Bitmap preViewImage150 = ImageProcessing.sumPixel(LogicHelper.toLogic,
				150);
		Bitmap preViewImage100 = ImageProcessing.sumPixel(LogicHelper.toLogic,
				100);
		Bitmap preViewImage50 = ImageProcessing.sumPixel(LogicHelper.toLogic,
				50);

		if (s.equals("2X2")) {
			imageView.setImageBitmap(imageResize(preViewImage50));
		} else if (s.equals("4X4")) {
			imageView.setImageBitmap(imageResize(preViewImage100));
		} else if (s.equals("6X6")) {
			imageView.setImageBitmap(imageResize(preViewImage150));
		} else {
		}

		return listView;
	}

	public Bitmap imageResize(Bitmap bitmap) {
		int x = 500, y = 500; // �ٲ� �̹��� ������
		Bitmap output = Bitmap.createBitmap(x, y, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		canvas.drawBitmap(bitmap, 0, 0, null);
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Rect src = new Rect(0, 0, w, h);
		Rect dst = new Rect(0, 0, x, y);// �� ũ��� �����
		canvas.drawBitmap(bitmap, src, dst, null);
		return output;
	}
}
